<?php
Zend_Loader::loadClass('Lead_Document_Part');
Zend_Loader::loadClass('Lead_Document_Body');
Zend_Loader::loadClass('Lead_Document_DTD');

Zend_Loader::loadClass('Lead_Document_Head');
Zend_Loader::loadClass('Lead_Document_Head_Part');
Zend_Loader::loadClass('Lead_Document_Head_Meta');
Zend_Loader::loadClass('Lead_Document_Head_Script');
Zend_Loader::loadClass('Lead_Document_Head_Style');


/**
 * XHMTL document abstraction. Not like DOM document, but something like that.
 * Eliminates the need for deep templates, handles dtd, head and everything but the stuff inside <body> tag.
 * Knows XHTML 1.0 and HTML 4.01, automatically breaks documents (XML declaration) for Internet Exploder.
 *
 * As a special bonus, it can turn all IE / non-Ubuntu users away! 17 r0><0rz!
 *
 * @package Core
 * @subpackage Document
 * 
 * @todo This seems like pure clean composite patternah. Make interface and shit.
 *
 */
class Lead_Document
{
	const HTML401_LOOSE = 0;
	const XHTML10_LOOSE = 1;
	const XHTML10_STRICT = 2;
	const XHTML11 = 3;

	/**
	 * Document type
	 *
	 * @var integer
	 */
	protected $type = 1;
	/**
	 * Document's content type (default 'UTF-8')
	 *
	 * @var unknown_type
	 */
	protected $content_type = 'UTF-8';

	/**
	 * Document DTD
	 *
	 * @var Document_DTD
	 */
	public $dtd;
	/**
	 * Document head
	 *
	 * @var Document_Head
	 */
	public $head;
	/**
	 * Document body
	 *
	 * @var Document_Body
	 */
	public $body;

	/**
	 * Full document
	 *
	 * @var string
	 */
	protected $full;
	/**
	 * Is the document fully constructed?
	 *
	 * @var boolean
	 */
	protected $_constructed;

	/**
	 * Is user's browser shitty?
	 *
	 * @var boolean
	 */
	protected $is_ie = false;
	/**
	 * Fix output for shitty browsers.
	 *
	 * @var unknown_type
	 */
	protected $fix_ie = true;


	/**
	 * Document parts (dtd, html_start, head, body, html_end)
	 *
	 * @var array
	 */
	protected $parts = array();

	/**
	 * Document weight in bytes
	 *
	 * @var integer
	 */
	protected $weight;

	/**
	 * Initializes the document
	 *
	 * @param Application $lead
	 */
	function __construct()
	{

		$this->is_ie = $this->sniffBrowser();

		$this->dtd = new Lead_Document_DTD($this);
		$this->head = new Lead_Document_Head($this);
		$this->body = new Lead_Document_Body($this);


	}


	function type()
	{
		return $this->type;
	}

	/**
	 * Returns document's content-type
	 *
	 * @return unknown
	 */
	function contentType()
	{
		return $this->content_type;
	}



	/**
	 * Renders the document
	 *
	 * @return string
	 */
	function render()
	{
		$this->head->meta->add('content-type', "text/html; charset={$this->content_type}", 1);
	    
	    if(!$this->_constructed)
		{
			$this->construct();
		}

		return $this->full;


	}


	/**
	 * Enter description here...
	 *
	 */
	function construct()
	{
		$this->parts['dtd'] = $this->dtd->render();
		$this->parts['html_start'] = ($this->type) ? "<html xmlns=\"http://www.w3.org/1999/xhtml\">" : "<html>";
		$this->parts['head'] = $this->head->render();
		$this->parts['body'] = $this->body->render();
		$this->parts['html_end'] = "</html>";
		$this->full = implode("\n", $this->parts);
		$this->weight = strlen($this->full);
	}





	function sniffBrowser()
	{
		if ($is_ie = strpos($_SERVER['HTTP_USER_AGENT'], "MSIE"))
			return true;
		return false;
	}


	/**
	 * Is user's browser shitty?
	 *
	 * @return unknown
	 */
	function isIE()
	{
		return $this->is_ie;
	}

	/**
	 * Returns IE fix status
	 *
	 * @return unknown
	 */
	function fixIE()
	{
		return $this->fix_ie;
	}


	function weight()
	{
		if(!$this->_constructed)
			$this->construct();

		return $this->weight;

	}


	/**
	 * Sets document title.
	 *
	 * @param string $title
	 */
	public function setTitle($title)
	{
		$this->head->setTitle($title);

	}
	
	
	public function guessTitle()
	{
	    $body = $this->body->get();

	    preg_match("/<h1>(.*?)<\/h1>/", $body, $matches);
	    
	    if($matches)
	        return $matches[1];
        	    
	}
	
	

	public function prependTitle($append)
	{
	    $this->head->setTitle($append . $this->head->getTitle());
	}

	/**
	 * Returns document title
	 *
	 * @return unknown
	 */
	public function getTitle()
	{
		return $this->head->getTitle();
	}



	public function setContentType($contentType)
	{
	    $this->content_type = $contentType;
	}

	
	
	public function __toString()
	{
	    return $this->render();
	}
	

}








?>